import { emitter } from '@/utils/bus'

export const fmtTitle = (title, now) => {
  if (!title) {
    return '';
  }

  const reg = /\${(.+?)}/;
  const reg_g = /\${(.+?)}/g;
  const result = title.match(reg_g);
  if (result) {
    result.forEach((item) => {
      const key = item.match(reg)[1];
      const value = now.params[key] || now.query[key];
      title = title.replace(item, value);
    });
  }
  return title;
};

export const closeCurrPage = () => {
  emitter.emit('closeCurrPage');
};

export const getPageTitle = (pageTitle, route) => {
  if (pageTitle) {
    return fmtTitle(pageTitle, route);
  }
  return '';
};

export default {
  closeCurrPage,
  getPageTitle,
  fmtTitle
};